package com.cx.controller.czw;

import com.cx.service.czw.SelectPageInfoService;
import com.cx.vo.ResponseResult;
import com.cx.vo.czw.UserDetailPageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Controller
@RequestMapping("takePage")
public class PageCodeController {
    @Autowired
    SelectPageInfoService selectPageInfoService;

    //查询所有快递信息
    @RequestMapping("selectAllPageInfo")
    @ResponseBody
    public ResponseResult selectAllPageInfo(){
        ResponseResult responseResult = new ResponseResult();
        List<UserDetailPageInfo> userDetailPageInfoList = selectPageInfoService.selectAllPageInfo();
        if(null != userDetailPageInfoList){
            responseResult.setMessage("查询全部快递成功");
            responseResult.setCode(1);
            responseResult.setData(userDetailPageInfoList);
        }else {
            responseResult.setMessage("查询全部快递失败");
            responseResult.setCode(2);
        }
        return responseResult;
    }
    //根据快递编号，或收件人电话或收件人姓名查询快递
    @RequestMapping("selectByConditions")
    @ResponseBody
    public ResponseResult selectByConditions(String searchValue){
        ResponseResult responseResult = new ResponseResult();
        String phoneRule = "^[\\d]{11}$";
        Pattern phonePattern = Pattern.compile(phoneRule);
        Matcher phoneMatcher = phonePattern.matcher(searchValue);

        String pageNumRule ="^[\\d]{9}$";
        Pattern pageNumPattern = Pattern.compile(pageNumRule);
        Matcher pageNumMatcher = pageNumPattern.matcher(searchValue);
        if(null == searchValue||"" == searchValue){
            List<UserDetailPageInfo> userDetailPageInfoList = selectPageInfoService.selectAllPageInfo();
            if(null != userDetailPageInfoList){
                responseResult.setMessage("查询全部快递成功");
                responseResult.setCode(1);
                responseResult.setData(userDetailPageInfoList);
            }else {
                responseResult.setMessage("查询全部快递失败");
                responseResult.setCode(2);
            }
        }else if(phoneMatcher.matches()){
            List<UserDetailPageInfo> userDetailPageInfoList = selectPageInfoService.selectPageByReceivePhone(searchValue);
            if(null != userDetailPageInfoList){
                responseResult.setMessage("根据收件人电话查询全部快递成功");
                responseResult.setCode(1);
                responseResult.setData(userDetailPageInfoList);
            }else {
                responseResult.setMessage("根据收件人电话查询全部快递失败");
                responseResult.setCode(2);
            }
        }else if(pageNumMatcher.matches()){
            List<UserDetailPageInfo> userDetailPageInfoList = selectPageInfoService.selectPageByPageNum(Integer.parseInt(searchValue));
            if(null != userDetailPageInfoList){
                responseResult.setMessage("根据快递编号查询全部快递成功");
                responseResult.setCode(1);
                responseResult.setData(userDetailPageInfoList);
            }else {
                responseResult.setMessage("根据快递编号查询全部快递失败");
                responseResult.setCode(2);
            }
        }else {
            List<UserDetailPageInfo> userDetailPageInfoList = selectPageInfoService.selectPageByReceiveName(searchValue);
            if(null != userDetailPageInfoList){
                responseResult.setMessage("根据收件人姓名查询全部快递成功");
                responseResult.setCode(1);
                responseResult.setData(userDetailPageInfoList);
            }else {
                responseResult.setMessage("根据收件人姓名查询全部快递失败");
                responseResult.setCode(2);
            }
        }
        return responseResult;
    }
}
